package com.eastdigit.system.service;

import com.eastdigit.servlet.ReqBean;
import com.eastdigit.system.inter.CodeMapper;
import com.eastdigit.system.model.BusinessException;
import com.eastdigit.system.model.Code;
import com.eastdigit.system.model.SqlBuilder;
import com.eastdigit.util.StringUtil;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CodeService extends BaseService implements ICodeService {

    @Autowired
    private CodeMapper codeMapper;

    private static final String CODE_TYPE = "codetype";

    /**
     * 查询
     * 
     * @param reqBean
     * @return
     */
    @Override
    public JSONObject queryTable(ReqBean reqBean) {
        SqlBuilder sql = new SqlBuilder("select * from SYS_CODE Where 1=1 and CODE_TYPE <> '" + CODE_TYPE + "' ")
                .addWhere("PARENT_CODE_ID", reqBean.getInfo("codeId"));
        return queryTable(sql, reqBean.getPageSize(), reqBean.getPageIndex());
    }

    /**
     * code类型树
     * 
     * @return
     */
    @Override
    public JSONArray queryCodeTypeTree() {
        SqlBuilder sql = new SqlBuilder("select * from SYS_CODE Where CODE_TYPE = '" + CODE_TYPE + "'");
        return query(sql);
    }

    @Override
    public JSONObject queryCodes() {
        JSONObject jsonRet = new JSONObject();
        List<Code> list = codeMapper.selectAllCodes();
        for (Code code : list) {
            String codeType = code.getCodeType();
            JSONArray jsonArray = jsonRet.optJSONArray(codeType);

            JSONObject json = new JSONObject();
            json.put("name", code.getCodeName());
            json.put("value", code.getCodeValue());
            if (jsonArray == null) {
                jsonArray = new JSONArray();
                jsonArray.add(json);
                jsonRet.put(codeType, jsonArray);
            } else {
                jsonArray.add(json);
            }
        }
        return jsonRet;
    }

    /**
     * 新增
     * 
     * @param code
     */
    @Override
    public String add(Code code) {
        if (this.executeInt("select count(0) from SYS_CODE where CODE_TYPE = ? and CODE_VALUE = ?", code.getCodeType(),
                code.getCodeValue()) > 0) {
            throw new BusinessException("字典值不能重复！");
        }
        code.setCodeId(intIncrementId("SYS_CODE", "CODE_ID"));
        codeMapper.insert(code);
        return code.getCodeId();
    }

    /**
     * 修改
     * 
     * @param code
     */
    @Override
    public String update(Code code) {
        String codeId = code.getCodeId();
        if (StringUtil.isEmpty(codeId)) {
            throw new BusinessException("获取主键失败");
        }
        if (this.executeInt("select count(0) from SYS_CODE where CODE_ID != ? and CODE_TYPE = ? and CODE_VALUE = ?",
                code.getCodeId(), code.getCodeType(), code.getCodeValue()) > 0) {
            throw new BusinessException("字典值不能重复！");
        }

        if (CODE_TYPE.equals(code.getCodeType())) {// 字典类型
            Code code1 = codeMapper.selectByPrimaryKey(codeId);
            if (!code.getCodeValue().equals(code1.getCodeValue())) {// 字典类型的type改变了
                if (this.executeInt("select count(0) from SYS_CODE where CODE_TYPE = ?", code1.getCodeValue()) > 0) {
                    throw new BusinessException("请先删除字典值！");
                }
            }
        }
        codeMapper.updateByPrimaryKeySelective(code);
        return code.getCodeId();
    }

    /**
     * 删除
     * 
     * @param codeId
     * @throws Exception
     */
    @Override
    public void delete(String codeId) {
        if (StringUtil.isEmpty(codeId)) {
            throw new BusinessException("获取主键失败");
        }
        Code code = codeMapper.selectByPrimaryKey(codeId);
        if (CODE_TYPE.equals(code.getCodeType())) {// 删除字典类型
            if (this.executeInt("select count(0) from SYS_CODE where CODE_TYPE = ?", code.getCodeValue()) > 0) {
                throw new BusinessException("请先删除字典值！");
            }
            if (this.executeInt("select count(0) from SYS_CODE where PARENT_CODE_ID = ?", codeId) > 0) {
                throw new BusinessException("请先删除子类型！");
            }
        }

        codeMapper.deleteByPrimaryKey(codeId);
    }

}
